package com.dldata.devtools30.controller.api;

import com.dldata.devtools30.entity.DevtApiTypeEntity;
import com.dldata.devtools30.repository.api.DevtApiTypeEntityDao;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/V_DevtApiTypeController")
public class V_DevtApiTypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    DevtApiTypeEntityDao devtApiTypeEntityDao;

    @RequestMapping(value = "/toPage")
    public String toPage(){
        return "thymeleaf/devtools30/api/v_DevtApiType";
    }

    @RequestMapping(value = "/edit")
    public String edit(String id,Model model){
        DevtApiTypeEntity pFunctiontypeEntity = null;
        if(!StringUtil.isNullOrSpace(id)){
            pFunctiontypeEntity = devtApiTypeEntityDao.findById(id).orElse(null);
        }
        if (pFunctiontypeEntity == null) {
            pFunctiontypeEntity = new DevtApiTypeEntity();
            pFunctiontypeEntity.setId(UUID.randomUUID().toString());
            
            List<DevtApiTypeEntity> pFunctiontypeEntities =new ArrayList() ;
            Iterable<DevtApiTypeEntity> typeEntityDaoAll = devtApiTypeEntityDao.findAll();
            Iterator<DevtApiTypeEntity> entityIterator = typeEntityDaoAll.iterator();
            while (entityIterator.hasNext()) {
                DevtApiTypeEntity devtApiTypeEntity = entityIterator.next();
                pFunctiontypeEntities.add(devtApiTypeEntity);
            }
            pFunctiontypeEntity.setXh(pFunctiontypeEntities.size()+1);
            for (int i = 0; i < pFunctiontypeEntities.size(); i++) {
                DevtApiTypeEntity functionmainEntity = pFunctiontypeEntities.get(i);
                if(functionmainEntity.getXh()!=null&&functionmainEntity.getXh()>=pFunctiontypeEntity.getXh()){
                    pFunctiontypeEntity.setXh(functionmainEntity.getXh()+1);
                }
            }
        }
        model.addAttribute("data",pFunctiontypeEntity);
        return "thymeleaf/devtools30/api/v_DevtApiTypeEdit";
    }
}
